//
//  IconPagesTwoViewController.m
//  homeland
//
//  Created by caesar on 14-7-31.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//

#import "IconPagesTwoViewController.h"
#import "IconButton.h"
#import <objc/runtime.h>
#import "ListViewController.h"
#import "UIColor+expanded.h"

#define ICON_MARGINS 10.0
#define ICON_SPACES 15.0
#define ICON_WIDTH 90.0
#define ICON_HEIGHT 110.0

@interface IconPagesTwoViewController ()

@end

@implementation IconPagesTwoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self drawIcons];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void) drawIcons
{
    NSString *title = [_config objectForKey:@"title"];
    self.title = title;
    NSArray *iconConfigs = [_config objectForKey:@"items"];
    for (int i = 0; i < [iconConfigs count]; i ++) {
        NSDictionary *iconConfig = [iconConfigs objectAtIndex:i];
        CGFloat iconX = ICON_MARGINS + i%3*(ICON_WIDTH+ICON_SPACES);
        CGFloat iconY = ICON_MARGINS + i/3*(ICON_HEIGHT+ICON_SPACES);
        CGRect iconRect = CGRectMake(iconX, iconY, ICON_WIDTH, ICON_HEIGHT);
        IconButton *button = [[[NSBundle mainBundle]loadNibNamed:@"IconButton" owner:self options:nil] objectAtIndex:0];
        //        IconButton *button = [[IconButton alloc]initWithFrame:iconRect];
        button.frame = iconRect;
        NSString *imageName = [iconConfig objectForKey:@"image"];
        NSLog(@"imageName = %@",imageName);
        
        button.iconImageView.image = [UIImage imageNamed:imageName];
        NSString *buttonTitle = [iconConfig objectForKey:@"name"];
        button.iconTitleLabel.text = buttonTitle;
        button.targetStr = [iconConfig objectForKey:@"target"];
        button.userInteractionEnabled = YES;
        //初始化触摸事件
        [button addTarget:self action:@selector(tapIconEvent:) forControlEvents:UIControlEventTouchUpInside];
        [button addTarget:self action:@selector(iconTouchedDown:) forControlEvents:UIControlEventTouchDown];
        [button addTarget:self action:@selector(iconTouchCancel:) forControlEvents:UIControlEventTouchCancel];
        [button addTarget:self action:@selector(iconTouchCancel:) forControlEvents:UIControlEventTouchDragExit];
        [self.scrollView addSubview:button];
    }
    //控制scroll content的高度
    int numOfLines = ceil([iconConfigs count]/3.0);
    CGFloat totalHeight = ICON_MARGINS*2+numOfLines*ICON_HEIGHT+(numOfLines-1)*ICON_SPACES;

    self.scrollView.contentSize = CGSizeMake(320.0, totalHeight);
}

//icon的点击事件
-(void)tapIconEvent:(id)sender
{
    IconButton *icon = (IconButton *)sender;
    NSLog(@"click icon:%@",icon.iconTitleLabel.text);
    icon.backgroundColor = [UIColor clearColor];
    //进入下一级页面
    NSString *value = icon.iconTitleLabel.text;
    NSMutableDictionary *vcConfig = [[NSMutableDictionary alloc] init];
    if ([icon.iconTitleLabel.text isEqualToString:@"居民-街道办事"]) {
        value = @"居民办事指南";
        [vcConfig setObject:[NSNumber numberWithBool:YES] forKey:@"Guide"];
        [vcConfig setObject:[NSNumber numberWithInt:0] forKey:@"GuideIndex"];
        
    }
    if ([icon.iconTitleLabel.text isEqualToString:@"居民-部门办事"]) {
        value = @"居民办事指南";
        [vcConfig setObject:[NSNumber numberWithBool:YES] forKey:@"Guide"];
        [vcConfig setObject:[NSNumber numberWithInt:1] forKey:@"GuideIndex"];
        
    }
    if ([icon.iconTitleLabel.text isEqualToString:@"企业-部门办事"]) {
        value = @"企业办事指南";
        [vcConfig setObject:[NSNumber numberWithBool:YES] forKey:@"Guide"];
        [vcConfig setObject:[NSNumber numberWithInt:1] forKey:@"GuideIndex"];
        
    }
    //如果是社区自治 -- 传递个性化参数
    NSString *keyTitle = [_config objectForKey:@"keyTitle"];
    if (keyTitle) {
        if ([keyTitle isEqualToString:@"社区自治"]) {
            [vcConfig setObject:[_config objectForKey:@"communityId"] forKey:@"communityId"];
            [vcConfig setObject:[_config objectForKey:@"partyMember"] forKey:@"partyMember"];
            [vcConfig setObject:[_config objectForKey:@"streetId"] forKey:@"streetId"];
        }

    }
    Class tempClass =  NSClassFromString(icon.targetStr);
    if (!tempClass) {
        NSLog(@"target%@类型不正确",icon.targetStr);
        return;
    }
    NSLog(@"tempClass = %@",tempClass);
    
    [vcConfig setObject:value forKey:@"key"];
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectIconViewControll:nib:config:)]) {
        [self.delegate didSelectIconViewControll:tempClass nib:icon.targetStr config:vcConfig];
        return;
    }
    ConfigableViewController *vc = [[tempClass alloc]initWithNibName:icon.targetStr bundle:nil];
    vc.config = vcConfig;
    [self.navigationController pushViewController:vc animated:YES];
}



//icon的按下事件，这里主要是变个颜色
-(void)iconTouchedDown:(id) sender
{
    IconButton *icon = (IconButton *)sender;
    icon.backgroundColor = [UIColor colorWithRed:0.50 green:0.68 blue:0.84 alpha:1.0];
    NSLog(@"icon %@ touch down",icon.iconTitleLabel.text);
}

//icon的取消事件，包含拖拽出控件，这里主要是变回颜色
-(void)iconTouchCancel:(id)sender
{
    IconButton *icon = (IconButton *)sender;
    icon.backgroundColor = [UIColor clearColor];
    NSLog(@"icon %@ touch cancel",icon.iconTitleLabel.text);
}
@end
